package com.archive.forum.controller;

import com.archive.forum.dto.PaginationDTO;
import com.archive.forum.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {
    //主页
    @Autowired
    private PostService postService;
    @GetMapping("/")
    public String index(Model model,
                        @RequestParam(name = "page",defaultValue = "1") Integer page,
                        @RequestParam(name = "size",defaultValue = "5") Integer size,
                        @RequestParam(name = "tab", defaultValue = "latest") String tab){
        // 获取热门标签
        List<String> hotTags = postService.getHotTags();
        model.addAttribute("hotTags", hotTags != null ? hotTags : Collections.emptyList());
        PaginationDTO paginationDTO;
        switch (tab) {
            case "latest":
                paginationDTO = postService.list(page, size);
                break;
            case "popular":
                // 修改为使用"total"参数
                paginationDTO = postService.listPopular(page, size, "total");
                break;
            case "weekly":
                paginationDTO = postService.listPopular(page, size, "weekly");
                break;
            case "monthly":
                paginationDTO = postService.listPopular(page, size, "monthly");
                break;
            case "oldest":
                // 如果决定保留listOldest方法则保持，否则移除这个case分支
                paginationDTO = postService.listOldest(page, size);
                break;
            default:
                paginationDTO = postService.list(page, size);
                tab = "latest";
        }

        model.addAttribute("paginationDTO", paginationDTO);
        model.addAttribute("activeTab", tab);
        return "index";
    }
    @GetMapping("/search")
    public String search(Model model,
                         @RequestParam(name = "keyword") String keyword,
                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                         @RequestParam(name = "size", defaultValue = "5") Integer size) {

        PaginationDTO paginationDTO = postService.search(keyword, page, size);
        model.addAttribute("paginationDTO", paginationDTO);
        model.addAttribute("keyword", keyword);
        return "index"; // 使用与首页相同的模板
    }
    @GetMapping("/search-tag/{tag}")
    public String searchtag(@PathVariable(name = "tag")String tag,Model model,
                            @RequestParam(name = "page", defaultValue = "1") Integer page,
                            @RequestParam(name = "size", defaultValue = "5") Integer size) {

        PaginationDTO paginationDTO = postService.searchtag(tag, page, size);
        model.addAttribute("paginationDTO", paginationDTO);
        model.addAttribute("tag", tag);
        return "index";
    }
}
